package xlcao.sohutv.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import xlcao.sohutv.ui.MeleTVMainActivity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Log;

public class FileOperator {

	private static String AppPath_prefix = getPackgetPath();

	private static FileOperator instance = new FileOperator();

	public static FileOperator getInstance() {
		return instance;
	}
	
	private static String getPackgetPath() {
		PackageManager pm = MeleTVMainActivity.getContext().getPackageManager();
		PackageInfo p = null;
		try {
			p = pm.getPackageInfo(MeleTVMainActivity.getContext().getPackageName(), 0);
			File packgetPath = new File(p.applicationInfo.dataDir);
			return packgetPath.getPath() + File.separator;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
    }
	public File setupWebHome() {
		File wwwroot = new File(AppPath_prefix + "wwwroot");
		File dbroot = new File(AppPath_prefix + "databases");
		if (!dbroot.exists()) {
			dbroot.mkdirs();
		}
		if (!wwwroot.exists()) {
			wwwroot.mkdirs();
			CopyAssets();
		}

		return wwwroot;

	}

	public boolean checkDBExist() {
		File dbfile = new File(AppPath_prefix + "databases//TVInfo.db");
		if (!dbfile.exists()) {
			CopyAssets();
			return false;
		}
		return true;
	}
	public void updateDB() {
		copyfile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()+"//TVInfo.db", AppPath_prefix
				+ "databases//TVInfo.db");
	}
	public void writeHTML(String html) {
		try {
			OutputStream out = new FileOutputStream(AppPath_prefix
					+ "wwwroot//index.html");

			out.write(html.getBytes());

			out.flush();
			out.close();
			out = null;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void CopyAssets() {
		AssetManager assetManager = MeleTVMainActivity.getContext().getAssets();
		String[] files = null;
		try {
			files = assetManager.list("");
		} catch (IOException e) {
			Log.e("tag", e.getMessage());
		}
		
		File dbroot = new File(AppPath_prefix + "databases");
		if (!dbroot.exists()) {
			dbroot.mkdirs();
		}
		for (String filename : files) {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = assetManager.open(filename);

				out = new FileOutputStream(AppPath_prefix + "databases//"
						+ filename);

				copyFile(in, out);
				in.close();
				in = null;
				out.flush();
				out.close();
				out = null;
			} catch (Exception e) {
				Log.e("tag", e.getMessage());
			}
		}
	}

	private void copyFile(InputStream in, OutputStream out) throws IOException {
		byte[] buffer = new byte[1024];
		int read;
		while ((read = in.read(buffer)) != -1) {
			out.write(buffer, 0, read);
		}
	}

	private void copyfile(String srFile, String dtFile) {

		InputStream in = null;
		OutputStream out = null;
		try {
			in = new FileInputStream(srFile);
			out = new FileOutputStream(dtFile);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			copyFile(in, out);
			in.close();
			in = null;
			out.flush();
			out.close();
			out = null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
